package com.lwlk.zdk.controller.Platform;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.mapper.T.TDisPlatformMapper;
import com.lwlk.zdk.mapper.pw.pwMenuMapper;
import com.lwlk.zdk.mapper.pw.pwPlatformMenuRoleMapper;
import com.lwlk.zdk.mapper.pw.pwUserMapper;
import com.lwlk.zdk.model.CommonUser;
import com.lwlk.zdk.model.T.TDisPlatform;
import com.lwlk.zdk.model.pw.pwPlatformMenuRole;
import com.lwlk.zdk.model.task.pwTaskEnterpriseAssessmentStatistics;
import com.lwlk.zdk.util.JwtTokenUtil;
import com.lwlk.zdk.util.LayuiTablePagingResult;
import com.lwlk.zdk.util.Response;
import com.lwlk.zdk.util.listerUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping(value = "/userRole")
public class PlatformRoleMenuController {

    @Autowired
    pwPlatformMenuRoleMapper platformMenuRoleMapper;
    @Autowired
    pwMenuMapper pwMenuMapper;
    @Autowired
    TDisPlatformMapper disPlatformMapper;
    @Autowired
    pwUserMapper pwUserMapper;


    @ApiOperation(value = "用户权限添加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Response getAdd(@RequestBody pwPlatformMenuRole platformMenuRole, HttpServletRequest request){
        Response response=new Response();
        Boolean flag= listerUtil.getRoleType(request);
        if (flag==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此账号为监管员账号,不可进行操作");
            return response;
        }
        Boolean flage=listerUtil.getLevel(request);
        if (flage==false) {
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("该账号没有此权限");
            return response;
        }
        int i=platformMenuRoleMapper.insertSelective(platformMenuRole);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        response.setData(i);
        return response;
    }

    @ApiOperation(value = "用户权限修改")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Response getUpdate(@RequestBody pwPlatformMenuRole platformMenuRole, HttpServletRequest request){
        Response response=new Response();
        Boolean flag=listerUtil.getRoleType(request);
        if (flag==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此账号为监管员账号,不可进行操作");
            return response;
        }
        Boolean flage=listerUtil.getLevel(request);
        if (flage==false) {
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("该账号没有此权限");
            return response;
        }
        int i=platformMenuRoleMapper.updateByPrimaryKeySelective(platformMenuRole);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        response.setData(i);
        return response;
    }

    @ApiOperation(value = "用户权限删除")
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public Response getDelete(Integer id,HttpServletRequest request){
        Response response=new Response();
        Boolean flag=listerUtil.getRoleType(request);
        if (flag==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此账号为监管员账号,不可进行操作");
            return response;
        }
        Boolean flage=listerUtil.getLevel(request);
        if (flage==false) {
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("该账号没有此权限");
            return response;
        }
        int i=platformMenuRoleMapper.deleteByPrimaryKey(id);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        response.setData(i);
        return response;
    }

    @ApiOperation(value = "用户列表展示")
    @RequestMapping(value = "/listShow",method = RequestMethod.GET)
    public LayuiTablePagingResult getListShow(Integer regionCode,String roleName,Integer roleId,Integer page,Integer size){
        LayuiTablePagingResult result=null;
        PageHelper.startPage(page,size);
        List<Map<String,Object>>platformList=disPlatformMapper.selectPlatMenuList(String.valueOf(regionCode));
        PageInfo<pwTaskEnterpriseAssessmentStatistics> pageInfo=new PageInfo(platformList);
        result = new LayuiTablePagingResult();
        result.setCode(RetCode.SUCCESS.getCode());
        result.setMsg(RetCode.SUCCESS.getMsg());
        result.setCount(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        Long totalPages = pageInfo.getTotal() / size;
        if (pageInfo.getTotal() % size != 0){
            totalPages ++;
        }
        result.setTotalPages(totalPages);
        return result;
    }

    @ApiOperation(value = "用户权限展示")
    @RequestMapping(value = "/showDetail",method = RequestMethod.GET)
    public Response getShow(Integer uId){
     Response response=new Response();
      List<Map<String,Object>>platformRoleList =platformMenuRoleMapper.selectByExample(uId);
        for (int i = 0; i < platformRoleList.size(); i++) {
            Map map=platformRoleList.get(i);
            String[] menuIevelId= String.valueOf(map.get("menuIevelId")).split(",");//一级菜单
            String[] submenuId= String.valueOf(map.get("submenuId")).split(",");//二级菜单
            String[] threeLevelMenuId= String.valueOf(map.get("threeLevelMenuId")).split(",");//三级菜单
            ArrayList submenuList=new ArrayList();
            ArrayList threeLevelList=new ArrayList();
            ArrayList menuIevelList=new ArrayList();

            String submenuPath=new String();
            String submenuName=new String();
            String threeLevelMenuPath=new String();
            String threeLevelMenuName=new String();
            String menuIevelIdPath=new String();
            List<Map<String,Object>>superiorName=new LinkedList<>();
            String menuIevelName=new String();
            for (String sId : submenuId) {
                Map map1=new HashMap();
                if (sId.equals("")||sId.equals("null")){
                    continue;
                }
                submenuName=pwMenuMapper.selectSubmenuName(Integer.valueOf(sId));
                superiorName=pwMenuMapper.selectmenuIevelSignSuperior(Integer.valueOf(sId));
                map1.put("menuIevelName",String.valueOf(superiorName.get(0).get("menuIevelSign")));
                map1.put("submenuName",submenuName);
                submenuPath=pwMenuMapper.selectRoutingPath(Integer.valueOf(sId));
                map1.put("submenuPath",submenuPath);
                map1.put("icon","icon-sstj");
                map1.put("menuIevelId",String.valueOf(superiorName.get(0).get("menu_ievel_id")));
                submenuList.add(map1);
            }
            for (String tId : threeLevelMenuId) {
                if (tId.equals("")||tId.equals("null")){
                    continue;
                }
                Map map2=new HashMap();
                threeLevelMenuName=pwMenuMapper.selectThreeLevelMenuName(Integer.valueOf(tId));
                superiorName=pwMenuMapper.selectSuperior(Integer.valueOf(tId));
                map2.put("menuIevelName",String.valueOf(superiorName.get(0).get("menuIevelSign")));
                map2.put("submenuName",String.valueOf(superiorName.get(0).get("submenuSign")));
                map2.put("threeLevelMenuName",threeLevelMenuName);
                map2.put("submenuId",String.valueOf(superiorName.get(0).get("submenu_id")));

                threeLevelMenuPath=pwMenuMapper.selectRoutingPath(Integer.valueOf(tId));
                map2.put("threeLevelMenuPath",threeLevelMenuPath);
                threeLevelList.add(map2);
            }
            for (String mId : menuIevelId) {
                Map map3=new HashMap();
                if (mId.equals("")||mId.equals("null")){
                    continue;
                }
                menuIevelName=pwMenuMapper.selectMenuIevelName(Integer.valueOf(mId));
                map3.put("menuIevelName",menuIevelName);
                if (menuIevelName.equals("车辆监管")||menuIevelName.equals("平台监管")||menuIevelName.equals("统计分析")){
                    map3.put("sign","1");
                }
                menuIevelIdPath=pwMenuMapper.selectMenuIevelIdPath(Integer.valueOf(mId));
                Integer ievelId=pwMenuMapper.selectMenuIevelId(Integer.valueOf(mId));
                map3.put("menuIevelIdPath",menuIevelIdPath);
                map3.put("menuIevelId",ievelId);
                menuIevelList.add(map3);
            }
            map.put("submenu",submenuList);
            map.put("threeLevel",threeLevelList);
            map.put("menuIevelList",menuIevelList);

        }
        response.setData(platformRoleList);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
    }
}
